package cn.com.lms.business.livecourse.util;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.KeyStore;

/**
 * HttpClientUtil
 *
 * @author barry
 * @date 2020/3/20
 **/
public class HttpClientUtil {

    /**
     * @Title: getNewHttpClient
     * @Description: Methods Description
     * @param @return
     * @return HttpClient
     * @throws
     */

    private static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore
                    .getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new SSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                    params, registry);
            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

//    public static String runStart(String url,String xml) throws IOException {
//        HttpClient httpclient = getNewHttpClient();
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.addHeader("Content-Type","text/xml");
//        httpPost.addHeader("Authorization","Basic T2JpeFVzZXI6T2JpeDEyMzQ1Ng==");
//        httpPost.setEntity(new StringEntity(xml));
//        HttpResponse response = httpclient.execute(httpPost);
//        String retSrc = EntityUtils.toString(response.getEntity());
//        httpPost.releaseConnection();
//        return retSrc;
//    }

    public static String post(String url,String body) throws IOException {
        HttpClient httpclient = getNewHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(body, "UTF-8"));
        HttpResponse response = httpclient.execute(httpPost);
        String retSrc = EntityUtils.toString(response.getEntity());
        httpPost.releaseConnection();
        return retSrc;
    }

    public static String get(String url) throws IOException {
        HttpClient httpclient = getNewHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);
        String retSrc = EntityUtils.toString(response.getEntity());
        httpGet.releaseConnection();
        return retSrc;
    }

}
